package actions;

//~--- non-JDK imports --------------------------------------------------------
import client.AnnAndWarn;
import client.TopicsPosts;
import client.UserModel;
import dto.Announcement;

import dto.JfMembers;
import dto.SectionsHome;
import dto.UserWarning;
import java.util.List;
import java.util.Vector;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

//~--- JDK imports ------------------------------------------------------------

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginAction extends org.apache.struts.action.Action {

    public LoginAction() {
    }

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {

        // TODO: Write method body
        ActionForward forward = new ActionForward();
        ActionErrors errors = new ActionErrors();
        String name = ((beans.LoginActionForm) form).getUsername();
        String pw = ((beans.LoginActionForm) form).getPassword();
 
        System.out.println("from beanform pw =" + pw);

        dto.JfMembers memb = new dto.JfMembers();

        memb.setUsername(name);
        memb.setPassword(pw);

        UserModel user = new UserModel();

        System.out.println("after create model in action");

        JfMembers member = user.login(memb);
       
        System.out.println("hgfhgs" + member.getPassword());
        String obtainhash = member.getPassword();
        String hash = UserModel.byteArrayToHexString(UserModel.computeHash(memb.getPassword())); // input from user        
        if (member.getMemberId() == null) {
            System.out.println("user not found in action");
            errors.add("UserNameErr", new ActionMessage("InvalidUserName"));
            saveErrors(request, errors);
            forward = mapping.findForward("failure");
        } else {
           
            if (hash.equals(obtainhash)) {
                System.out.println("success login");
                request.getSession(true).setAttribute("userBean", member);
                
                
                //========================Announcement============================
                AnnAndWarn announcement=new AnnAndWarn();
                Vector<Announcement> announcement1 = announcement.getannouncement();
                request.getSession(false).setAttribute("announcement",announcement1);
                System.out.println("After Announcement"+announcement1.size());
                //================================================================
                //======================Warning===================================
                Vector<UserWarning> warn = announcement.getwarn(member);
                 request.getSession(false).setAttribute("warn",warn);
                  System.out.println("After Warning"+warn.size());
                //===========================Get Sections==========================
                   System.out.println("Before Get All Sections");
                    TopicsPosts topic=new TopicsPosts();
                     List sections = topic.getallsections();
                    request.getSession(false).setAttribute("sections",sections);
                    System.out.println("sections are "+sections.size());
                System.out.println("After Get All Sections");
                //================================================================
                forward = mapping.findForward("success");
            } else {
                System.out.println("error password");
                errors.add("PasswordErr", new ActionMessage("InvalidPassword"));
                saveErrors(request, errors);
                forward = mapping.findForward("failure");
            }
        }
        return forward;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
